package com.parkreport.report.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.xml.rpc.ServiceException;

import org.jfree.data.time.Year;

import com.common.jdbc.DB4ObjUtil;
import com.common.report.service.ReportService;
import com.common.report.vo.ReportCountDataVO;
import com.common.report.vo.ReportDataVO;
import com.common.report.vo.ReportTitleVO;
import com.common.util.CalendarUtil;
import com.common.util.StringUtil;
import com.parkreport.vo.ReportParamVO;

public class YearMonthCarReportService extends ReportService {


	List<ReportCountDataVO> subList = new ArrayList<ReportCountDataVO>();
	private String type = "yearMonthCar";
	@Override
	public List findDatas(HttpServletRequest req) throws ServiceException {
		
		List<ReportCountDataVO> list = new ArrayList<ReportCountDataVO>();
		CalendarUtil calUtil = new CalendarUtil();
		String thisYear = String.valueOf(calUtil.getYear());
		list =findDatas(year);
		/*if(thisYear.equals(year)){
			list =findDatas(year);
		}else{
			list = findDatesByType(type,date);
		}*/
		return list;
	}
	public List findDatas(String date)  {
		
		StringBuilder sql = new StringBuilder("");
		//oracle to_char() mysql: cast(str as char)
		sql.append("select pa.AREA_NAME as ments, substr(lpr.RQSJ,6,2) as titles, count(*) as counts ");
		sql.append(" from LPR_RECORD lpr,CAR_AREA ca,QUERY_DMB qd,PARK_DMB pd,PARK_AREA pa");
		sql.append(" where 1=1");
		sql.append(" and pd.TYPE='1'");
		sql.append(" and substr(lpr.PLATENO,1,2) =ca.carNum");
		sql.append(" and lpr.NID = qd.NID ");
		sql.append(" and qd.NID = pd.NID");
		sql.append(" and pd.PARK_AREA_ID=pa.ID");
		sql.append(" and lpr.RQSJ like '"+date+"%'");
		sql.append(" group by substr(lpr.RQSJ,6,2), pa.AREA_NAME");
		
		log.info("sql=========="+sql);
		List<ReportCountDataVO> list = DB4ObjUtil.execute4Obj(new ReportCountDataVO(), sql.toString());
		return list;
	}
	
	public void excuteTimer(String date){
		List<ReportDataVO> list =findDatas(date);
		saveDates(list,type,date);
		
	}
	
	/*public List findSubDatas() throws ServiceException {
		StringBuilder sql = new StringBuilder("");
		//oracle to_char() mysql: cast(str as char)
		sql.append("select pa.AREA_NAME as ments,'小计' as titles, count(*) as counts ");
		sql.append(" from LPR_RECORD lpr,CAR_AREA ca,QUERY_DMB qd,PARK_DMB pd,PARK_AREA pa");
		sql.append(" where 1=1");
		sql.append(" and pd.TYPE='1'");
		sql.append(" and substr(lpr.PLATENO,1,2) =ca.carNum");
		sql.append(" and lpr.NID = qd.NID ");
		sql.append(" and qd.NID = pd.NID");
		sql.append(" and pd.PARK_AREA_ID=pa.ID");
		sql.append(" and lpr.RQSJ like '"+this.getYear()+"%'");
		sql.append(" group by pa.AREA_NAME");
		
		log.info("sql=========="+sql);
		subList = DB4ObjUtil.execute4Obj(new ReportCountDataVO(), sql.toString());
		list.addAll(findtotal(req));
		list.addAll(findsubtotal(req));
		list.addAll(findalltotal(req));
		return null;
	}*/
	
	
	@Override
	public List<ReportTitleVO> findTitles(HttpServletRequest req) throws ServiceException {
		StringBuilder sql = new StringBuilder("");
	//sql.append("select AREA_NAME as code,AREA_NAME as name from park_area where PID<>'0'");
		List<ReportTitleVO> list = new ArrayList<ReportTitleVO>();
		//list.add(new ReportTitleVO("小计","小计"));
		list.add(new ReportTitleVO("1月","01") );
		list.add(new ReportTitleVO("2月","02") );
		list.add(new ReportTitleVO("3月","03") );
		list.add(new ReportTitleVO("4月","04") );
		list.add(new ReportTitleVO("5月","05") );
		list.add(new ReportTitleVO("6月","06") );
		list.add(new ReportTitleVO("7月","07") );
		list.add(new ReportTitleVO("8月","08") );
		list.add(new ReportTitleVO("9月","09") );
		list.add(new ReportTitleVO("10月","10") );
		list.add(new ReportTitleVO("11月","11") );
		list.add(new ReportTitleVO("12月","12") );
		
		return list;
	}
	
	@Override
	public List<ReportTitleVO> findMents(HttpServletRequest req) throws ServiceException {
		StringBuilder sql = new StringBuilder("");
		sql.append("select AREA_NAME as code,AREA_NAME as name from park_area where PID<>'0'");
		List<ReportTitleVO> list = DB4ObjUtil.execute4Obj(new ReportTitleVO(), sql.toString());
	
		for (int i = 0; i < list.size(); i++) {
			ReportTitleVO v = list.get(i);
			String u = String.valueOf(mentMap.get("小计_"+v.getCode()));
			v.setUnit(u);
		}
		Collections.sort(list,new Comparator<ReportTitleVO>(){
			public int compare(ReportTitleVO obj1,ReportTitleVO obj2){
				if(StringUtil.isBlank(obj1.getUnit())&&StringUtil.isBlank(obj2.getUnit())){
					return 0;
				}
				//return order1-order2;
				if(StringUtil.isBlank(obj1.getUnit())){
					return 1;
				}
				
				if(StringUtil.isBlank(obj2.getUnit())){
					return -1;
				}
				int order1 = Integer.parseInt(obj1.getUnit());
				int order2 = Integer.parseInt(obj2.getUnit());
				return order2-order1;
			}
		});
		//list.add(new ReportTitleVO("合   计","合计"));
		return list;
	}
}
